'''
Created on 2011-04-05

@author: jasisz
'''
import sys
from PyQt4 import QtGui, QtCore
from dialogs import dialogs
from gameObjects import Resource, Field, Game
from mainWindow import Ui_MainWindow
import yaml

class MainWindow(QtGui.QMainWindow):

    def __init__(self):
        QtGui.QMainWindow.__init__(self)
        
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        #self.scene = QtGui.QGraphicsScene()
        self.game = Game("newGame")
        #self.resources = []
        self.fields = []
        self.rules = []
        
    def getFieldByName(self, name):
        print("Looking for " + name)
        for i in self.fields:
            if i.name == name:
                return i
        else:
            return None
        
    def getResourceByName(self, name):
        print("Looking for " + name)
        for j in self.fields:
            for i in j.resources:
                if i.name == name:
                    return i
                else:
                    return None
                
    def getListOfResourcesNames(self):
        lista = []
        for j in self.fields:
            try:
                for i in j.resources:
                    lista.append(i.name)
            except AttributeError:
                pass
        return list(set(lista))
                
    def getListOfResourcesTypes(self):
        lista = []
        for j in self.fields:
            try:
                for i in j.resources:
                    lista.append(i.type)
            except AttributeError:
                pass
        return list(set(lista))
                
    def getListOfFieldsNames(self):
        lista = []
        for j in self.fields:
            lista.append(j.name)
        return list(set(lista))
    
    @QtCore.pyqtSlot()
    def on_actionResize_activated(self):
        (x,y) = dialogs.showResizeDialog(self.ui.graphicsView.size())
        self.ui.graphicsView.setFixedSize(x, y)
        print(x,y)
        
    @QtCore.pyqtSlot()
    def on_actionLoad_background_activated(self):
        #TODO: dodac kopiowanie pliku do directory gry... ??
        fileName = QtGui.QFileDialog.getOpenFileName(self,"Open Image", "/home/jasisz/", "Image Files (*.png *.jpg *.bmp)")
        self.ui.graphicsView.setBackground(QtGui.QPixmap(fileName))
       
    @QtCore.pyqtSlot()
    def on_actionNew_field_activated(self):
        field = dialogs.showFieldDialog(self,self.ui.graphicsView.size(), Field())
        self.fields.append(field)
        self.ui.fieldsView.addItem(QtGui.QListWidgetItem(field.name))
        
    @QtCore.pyqtSlot()
    def on_editField_clicked(self):
        field = self.getFieldByName(str(self.ui.fieldsView.currentItem().text()))
        newfield = dialogs.showFieldDialog(self, self.ui.graphicsView.size(), field)
        if newfield:
            self.fields.remove(field)
            self.ui.fieldsView.takeItem(self.ui.fieldsView.currentRow())
            self.fields.append(newfield)
            self.ui.fieldsView.addItem(QtGui.QListWidgetItem(newfield.name))  
            
    @QtCore.pyqtSlot()
    def on_actionNew_rule_activated(self):
        rule = dialogs.showRuleDialog(self)
        self.rules.append(rule)
        self.ui.rulesView.addItem(QtGui.QListWidgetItem(rule.name))         
        
if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    m = MainWindow()
    m.show()
    app.exec_()
        
        